// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.rcp.intro;

import org.dengues.core.DenguesCorePlugin;
import org.dengues.designer.ui.login.LoginDenguesDialog;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

/**
 * This class controls all aspects of the application's execution
 */
/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-3-19 qiang.zhang $
 * 
 */
public class Application implements IApplication {

    private final static String firstTime = "org.dengues.rcp.intro.Application.firstTime";

    private Shell shell;

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "showLoginDialog".
     * 
     * @param shell
     * @return
     */
    private boolean showLoginDialog(Shell shell) {
        int di = DenguesCorePlugin.getDefault().getPreferenceStore().getInt(firstTime);
        if (di == 0) {
            DenguesCorePlugin.getDefault().getDesignerCoreService().importDemoProject(shell, new NullProgressMonitor());
            DenguesCorePlugin.getDefault().getPreferenceStore().setValue(firstTime, 1);
        }
        boolean logged = false;
        LoginDenguesDialog loginDialog = new LoginDenguesDialog(shell);
        logged = loginDialog.open() == LoginDenguesDialog.OK;
        return logged;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
     */
    public Object start(IApplicationContext context) throws Exception {
        Display display = PlatformUI.createDisplay();
        try {
            shell = new Shell(display, SWT.ON_TOP);
            try {
                if (!showLoginDialog(shell)) {
                    Platform.endSplash();
                    return EXIT_OK;
                }
            } finally {
                if (shell != null) {
                    shell.dispose();
                }
            }
            int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
            if (returnCode == PlatformUI.RETURN_RESTART) {
                return EXIT_RESTART;
            }
            return EXIT_OK;
        } finally {
            display.dispose();
        }
    }

    public void stop() {

    }
}
